# -*- coding:utf8 -*- #
# -----------------------------------------------------------------------------------
# ProjectName:  smalleel-api-tools
# FileName:     warehouse_acceptance.py
# Author  :
# Datetime:     2023/3/28 10:59
# Description：出入库方法
# -----------------------------------------------------------------------------------
from common.util.jsonUtil.jsonUtil import read_extract_json
from common.util.requestUtil.requestUtil import RequestsUtil
from common.util.timeUntil.timeUtil import get_nowtime
from tool.process.config.smalleelApi import PlatformApi, config
from tool.process.config.acceptConfig import AcceptanceConfig


class WarehouseAcceptance:
    def __init__(self):
        self.config = AcceptanceConfig()
        self.req = RequestsUtil()
        self.api = PlatformApi()
        self.username = config.get_user_info("usernamePlatform")
        self.plaform_token = read_extract_json('tokenPlatform', 6)

    def warehouse_in(self, modelId, devNo, devNum):
        """
        入库方法
        isBuy为是否买断，0为服务费项目，1为买断项目
        """
        isBuy = self.config.isBuy
        url = self.api.putStock
        headers = {"Token": self.plaform_token}
        if isinstance(devNo, int) and self.config.deviceType == 1:
            # 电表
            deviceType = 2
        elif self.config.deviceType == 6:
            if isinstance(devNo, int):
                # 设备是水表
                deviceType = 4
            else:
                # 设备是水表基站
                deviceType = self.config.deviceType
        else:
            # 基站
            deviceType = 1
        if devNum > 1:
            if isinstance(devNo, str):
                if isinstance(devNo[0], str):
                    devStart = devNo[1::]
                    devNoEnd = int(devStart) + int(devNum) - 1
                    devNo = f"{devNo}-{devNo[0]}{devNoEnd}"
            else:
                devNo = int(devNo)
                devNoEnd = int(devNo) + int(devNum) - 1
                devNo = f"{devNo}-{devNoEnd}"
        else:
            devNo = devNo
        detailList = [{"stockId": 1, "deviceType": deviceType, "modelId": modelId, "devNo": devNo,
                       "devNum": devNum}]
        params = {
            "detailList": f"{detailList}",
            "accessTypeId": 1,
            "optTime": get_nowtime(3),
            "userName": self.username,
            "isBuy": isBuy
        }
        response = self.req.send_request("post", url, params, parameterFormat="data", headers=headers)
        if response["code"] == 200:
            print(f"{devNo}入库成功！")
        else:
            print(f"{devNo}入库失败！失败原因: {response['msg']}")

    def warehouse_out(self, modelId, devNo, devNum):
        """
        出库方法
        """
        url = self.api.outStock
        headers = {"Token": self.plaform_token}
        if isinstance(devNo, int) and self.config.deviceType == 1:
            # 电表
            deviceType = 2
        elif self.config.deviceType == 6:
            if isinstance(devNo, int):
                # 设备是水表
                deviceType = 4
            else:
                # 设备是水表基站
                deviceType = self.config.deviceType
        else:
            # 基站
            deviceType = 1
        if devNum > 1:
            if isinstance(devNo, str):
                if isinstance(devNo[0], str):
                    devStart = devNo[1::]
                    devNoEnd = int(devStart) + int(devNum) - 1
                    devNo = f"{devNo}-{devNo[0]}{devNoEnd}"
            else:
                devNo = int(devNo)
                devNoEnd = int(devNo) + int(devNum) - 1
                devNo = f"{devNo}-{devNoEnd}"
        else:
            devNo = devNo
        detailList = [{"stockId": 1, "deviceType": deviceType, "modelId": modelId, "devNo": devNo,
                       "devNum": devNum}]
        params = {
            "detailList": f"{detailList}",
            "accessTypeId": 5,
            "optTime": get_nowtime(3),
            "userName": self.username,
            "proid": self.config.proId
        }
        response = self.req.send_request("post", url, params, parameterFormat="data", headers=headers)
        if response["code"] == 200:
            print(f"{devNo}出库成功！")
        else:
            print(f"{devNo}出库失败！失败原因: {response['msg']}")


if __name__ == '__main__':
    ware = WarehouseAcceptance()
    ware.warehouse_in(20, "C55520230001", 5)
    ware.warehouse_out(20, "C55520230001", 5)
